package com.ygs.secs.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ygs.secs.model.Admin;
import com.ygs.secs.model.Enterprise;
import com.ygs.secs.service.AdminService;
import com.ygs.secs.service.EnterpriseService;
import jdk.nashorn.internal.runtime.ECMAException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员管理
 */

@Controller
@RequestMapping("/admin")
public class AdminMangerController {
    @Autowired
    AdminService adminService;
    @Autowired
    EnterpriseService enterpriseService;

    /**
     * 查询所有管理员
     *
     * @return查询结果
     */
    @RequestMapping("/getAdminByIdAndName")
    @ResponseBody
    public Map<String, Object> getAdminByIdAndName(Admin admin, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int limit) {
        Map<String, Object> adminData = new HashMap<>();
        try {
            //分页查询
            Page startPage = PageHelper.startPage(page, limit);
            List<Admin> adminList = adminService.getAdminByIdAndName(admin);
            if (adminList.size() > 0) {
                adminData.put("code", 200);
                adminData.put("msg", "查询管理员数据成功");
                adminData.put("data", adminList);
                adminData.put("count", startPage.getTotal());
            } else {
                adminData.put("code", 201);
                adminData.put("msg", "数据为null，请先添加");
            }
            return adminData;
        } catch (Exception e) {
            adminData.put("code", 500);
            adminData.put("msg", "system error");
            return adminData;
        }
    }

    /**
     * 根据id删除管理员信息
     *
     * @param adminId 学院ID
     * @return 删除结果
     */
    @RequestMapping(value = "/delAdminById", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delAdminById(@RequestParam("adminId") String adminId) {
        Map<String, Object> result = new HashMap<>();
        if (adminId.equals("root")) {
            result.put("code", 501);
            result.put("msg", "操作失败，不能删除根管理员");
            return result;
        } else {
            try {
                int res = adminService.deleteByPrimaryKey(adminId);
                if (res > 0) {
                    result.put("code", 200);
                    result.put("msg", "操作成功");
                } else {
                    result.put("code", 500);
                    result.put("msg", "操作失败");
                }
                return result;
            } catch (Exception e) {
                e.printStackTrace();
                result.put("code", 500);
                result.put("msg", "操作失败，请先删除所属信息");
                return result;
            }
        }
    }

    /**
     * 修改管理员信息
     *
     * @param admin 管理员实体
     * @return 操作结果
     */
    @RequestMapping(value = "/updateAdmin", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateAdmin(Admin admin) {
        Map<String, Object> result = new HashMap<>();
        if (!admin.getAdminid().equals("root")) {
            int res = adminService.updateByPrimaryKeySelective(admin);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
        } else {
            result.put("code", 501);
            result.put("msg", "操作失败，不能修改根管理员");
        }
        return result;
    }

    /**
     * 添加管理员信息
     *
     * @param admin 管理员实体
     * @return 操作结果
     */
    @RequestMapping(value = "/addAdmin", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addAdmin(Admin admin) {
        Map<String, Object> result = new HashMap<>();
        try {
            int res = adminService.insert(admin);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        } catch (Exception e) {
            result.put("code", 501);
            result.put("msg", "操作失败，主键冲突，请先修改重复ID");
            return result;
        }
    }

    /**
     * 批量修改管理员信息
     *
     * @param jsonArrayAdmin 管理员实体json数组串
     * @return 操作结果
     */
    @RequestMapping(value = "/batchUpdateAdmin", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchUpdateAdmin(String jsonArrayAdmin) {
        Map<String, Object> result = new HashMap<>();
        List<Admin> admins = JSONArray.parseArray(jsonArrayAdmin, Admin.class);
        for (Admin admin : admins) {
            if (admin.getAdminid().equals("root")) {
                result.put("code", 501);
                result.put("msg", "操作失败，不能修改根管理员");
                return result;
            }
        }
        int res = adminService.updateBatchSelective(admins);
        if (res > 0) {
            result.put("code", 200);
            result.put("msg", "操作成功");
        } else {
            result.put("code", 500);
            result.put("msg", "操作失败");
        }
        return result;
    }

    /**
     * 批量添加管理员信息
     *
     * @param jsonArrayAdmin 管理员数组json串
     * @return 操作结果
     */
    @RequestMapping(value = "/batchInsertAdmin", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchInsertAdmin(String jsonArrayAdmin) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Admin> admins = JSONArray.parseArray(jsonArrayAdmin, Admin.class);
            int res = adminService.batchInsert(admins);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 501);
            result.put("msg", "操作失败，主键冲突，请先修改重复ID");
            return result;
        }
    }

    /**
     * 批量删除管理员信息
     *
     * @param admins 管理员id数组
     * @return 操作结果
     */
    @RequestMapping(value = "/batchDelAdmin", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchDelAdmin(String[] admins) {
        Map<String, Object> result = new HashMap<>();
        try {
            for (String adminId : admins) {
                if (adminId.equals("root")) {
                    result.put("code", 501);
                    result.put("msg", "操作失败，不能删除根管理员");
                    return result;
                }
            }
            int res = adminService.batchDelete(admins);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败，请先删除所属信息");
            return result;
        }
    }

    @RequestMapping("/findenterpriseQualificationlist")
    @ResponseBody
    public Map<String, Object> findenterpriseQualificationlist(int page, int limit) {
        Map<String, Object> map = new HashMap<>();
        try {
            //分页查询
            Page startPage = PageHelper.startPage(page, limit);
            List<Enterprise> enterpriseList = enterpriseService.findUncertifiedCompanies("1");
            if (enterpriseList != null) {
                map.put("code", 200);
                map.put("msg", "查询成功");
                map.put("count", startPage.getTotal());
                map.put("data", enterpriseList);
            } else {
                map.put("code", 201);
                map.put("msg", "查询失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "查询失败");
            map.put("data", null);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 批量修改认证状态
     *
     * @param commpanys 批量数据
     * @param status    状态码
     * @return json
     */
    @RequestMapping("/batchReviewCompanies")
    @ResponseBody
    public Map<String, Object> batchReviewCompanies(String[] commpanys, String status) {
        Map<String, Object> map = new HashMap<>();
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            int res = enterpriseService.batchReviewCompanies(commpanys, status, sf.format(new Date()));
            if (res > 0) {
                map.put("code", 200);
                map.put("msg", "操作成功");
            } else {
                map.put("code", 201);
                map.put("msg", "操作失败");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
        }
        return map;
    }
}
